import { createRouter, createWebHistory } from 'vue-router'
import VueCookies from 'vue-cookies'
const routes = [
  {
    name: 'Home',
    path: '/',
    meta: {
      title: '系统后台'
    },
    component: () => import('@/views/Home.vue'),
    children: [
      {
        name: 'blog',
        path: 'blog',
        icon: 'icon-blog',
        meta: {
          title: '博客'
        },
        children: [
          {
            path: '/blogManage',
            meta: {
              title: '博客管理'
            },
            component: () => import('@/views/blog/BlogList.vue')
          },
          {
            path: '/blogCategory',
            meta: {
              title: '分类管理'
            },
            component: () => import('@/views/blog/BlogCategory.vue')
          },

        ]
      },
      {
        path: 'special',
        icon: 'icon-remenzhuanti',
        meta: {
          title: '专题'
        },
        children: [
          {
            path: '/category',
            meta: {
              title: '专题管理'
            },
            component: () => import('@/views/special/Category.vue')
          }
        ]
      },
      {
        path: 'setting',
        icon: 'icon-setting-fill',
        meta: {
          title: '设置'
        },
        children: [
          {
            path: '/ownMsg',
            meta: {
              title: '个人信息设置'
            },
            component: () => import('@/views/setting/OwnMsg.vue')
          },
          {
            path: '/userMsg',
            meta: {
              title: '博客成员设置'
            },
            component: () => import('@/views/setting/UserMsg.vue')
          },
          {
            path: '/sysSetting',
            meta: {
              title: '系统设置'
            },
            component: () => import('@/views/setting/SysSetting.vue')
          }
        ]
      },
      {
        path: 'recovery',
        icon: 'icon-delete',
        meta: {
          title: '回收站'
        },
        children: [
          {
            path: '/recovery',
            meta: {
              title: '回收站'
            },
            component: () => import('@/views/recovery/Recovery.vue'),
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    meta: {
      title: '登录'
    },
    component: () => import('@/views/Login.vue')
  }
]

const router = createRouter({
  routes,
  history: createWebHistory()
})

// 前置路由守卫
router.beforeEach((to, from, next) => {
  const userInfo = VueCookies.get("userInfo")
  if (!userInfo && to.path != "/login") {
    router.push('/login')
  }
  next()
})
// 后置路由守卫
router.afterEach((to, from) => {
  document.title = to.meta.title
})
export default router